import argparse
import queue
import time
from http_server import create_http_server
from agent_go import run_command

def main() -> None:
    """
    Main entry point for the program. Parses command-line arguments and starts the HTTP server
    and Websocket client in separate threads.
    """
    parser = argparse.ArgumentParser(description="Control robotic arm via HTTP and WebSocket.")
    parser.add_argument("--ip", type=str, default="0.0.0.0", help="HTTP server IP address")
    parser.add_argument("--port", type=int, default=5000, help="HTTP server port")

    args = parser.parse_args()

    # Initialize shared resources
    command_queue: queue.Queue = queue.Queue()
    result_queue: queue.Queue = queue.Queue()
    results_dict: dict[str, dict] = {}

    # Start HTTP server
    http_server = create_http_server(args.ip, args.port, command_queue, result_queue, results_dict)
    http_server.start()

    while True:
        command = command_queue.get()
        id = str(time.time())
        result_queue.put({"id": id, "command": command})
        run_command(command)
        results_dict[id] = {"id": id, "command": command, "code": 0, "message": f"{command} 执行成功"}

if __name__ == "__main__":
    main()
